﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ComicIsland.Common.Entities;
using Microsoft.Phone.Net.NetworkInformation;

/**
 * 创建时间：2014/12/15 14:55:56
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.Common
{
    public sealed class NetworkStatusHelper
    {
        public NetworkStatusHelper()
        {
        }

        public static bool IsNetworkAvailable
        {
            get
            {
                return DeviceNetworkInformation.IsNetworkAvailable;
            }
        }



        /// <summary>
        /// 调用示例方法
        /// </summary>
        /// <returns></returns>
        public async Task<DeviceNetworkInfo> InvokeSample(string requestUrl)
        {
            DeviceNetworkInfo result = await new NetworkStatusHelper().GetNetworkTypeAsync(requestUrl);
            return result;
        }

        /// <summary>
        /// 由原来的异步方法转成同步方法
        /// </summary>
        /// <returns></returns>
        public async Task<DeviceNetworkInfo> GetNetworkTypeAsync(string requestUrl)
        {
            DeviceNetworkInfo result = await Task.Factory.StartNew<DeviceNetworkInfo>(() =>
            {
                DeviceNetworkInfo netInfo = null;
                EventWaitHandle Wait = new AutoResetEvent(false);
                DnsEndPoint dnsEndPoint = new DnsEndPoint(RequestHost(requestUrl), 80);
                DeviceNetworkInformation.ResolveHostNameAsync(dnsEndPoint, new NameResolutionCallback(handle =>
                {
                    NetworkInterfaceInfo info = handle.NetworkInterface;
                    netInfo = ParserNetworkInfo(info);
                    Wait.Set();

                }), null);
                Wait.WaitOne();
                return netInfo;
            });
            return result;
        }

        private string RequestHost(string requestUrl)
        {
            try
            {
                return new Uri(requestUrl).DnsSafeHost;
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 转移成服务器需要的网络类型
        /// </summary>
        /// <param name="interfaceInfo"></param>
        /// <returns></returns>
        private DeviceNetworkInfo ParserNetworkInfo(NetworkInterfaceInfo interfaceInfo)
        {
            DeviceNetworkInfo networkInfo = new DeviceNetworkInfo()
            {
                InterfaceName = string.Empty,
                Description = string.Empty,
                CustomerNetworkType = DeviceNetworkType.None
            };
            if (interfaceInfo == null)
            {
                return networkInfo;
            }

            networkInfo.InterfaceName = interfaceInfo.InterfaceName;
            networkInfo.Description = interfaceInfo.Description;
            switch (interfaceInfo.InterfaceType)
            {
                case NetworkInterfaceType.MobileBroadbandCdma:
                case NetworkInterfaceType.MobileBroadbandGsm:
                    switch (interfaceInfo.InterfaceSubtype)
                    {
                        case NetworkInterfaceSubType.Cellular_GPRS:
                        case NetworkInterfaceSubType.Cellular_1XRTT:
                        case NetworkInterfaceSubType.Cellular_EDGE:
                            networkInfo.CustomerNetworkType = DeviceNetworkType.Mobile2G;
                            break;
                        case NetworkInterfaceSubType.Cellular_3G:
                        case NetworkInterfaceSubType.Cellular_EVDO:
                        case NetworkInterfaceSubType.Cellular_EVDV:
                        case NetworkInterfaceSubType.Cellular_HSPA:
                            networkInfo.CustomerNetworkType = DeviceNetworkType.Mobile3G;
                            break;
                        case NetworkInterfaceSubType.Cellular_LTE:
                        case NetworkInterfaceSubType.Cellular_EHRPD:
                            networkInfo.CustomerNetworkType = DeviceNetworkType.Mobile4G;
                            break;
                        default:
                            networkInfo.CustomerNetworkType = DeviceNetworkType.None;
                            break;
                    }
                    break;
                case NetworkInterfaceType.Ethernet:
                case NetworkInterfaceType.Wireless80211:
                    networkInfo.CustomerNetworkType = DeviceNetworkType.Wifi;
                    break;
                default:
                    networkInfo.CustomerNetworkType = DeviceNetworkType.None;
                    break;
            }
            return networkInfo;
        }
    }
}
